package queenChessProblem;

import genetic_algorithm.Chromosome;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

/**
 * Chromosome for representing a solution for the queens problem.
 * A chromosome holds the queen's layout on the board
 */
public class QueensChromosome implements Chromosome {

	List<Object> queensLocatoins;

	public QueensChromosome(List<Object> queensLocatoins) {
		this.queensLocatoins = queensLocatoins;
	}

	public QueensChromosome() {
		queensLocatoins = new LinkedList<Object>();
		Random random = new Random();
		for (int i = 0; i < 8; i++) {
			queensLocatoins.add(random.nextInt(8));
		}
	}

	@Override
	public Integer getValue(int index) throws IllegalArgumentException {
		return (Integer) queensLocatoins.get(index);
	}

	@Override
	public List<Object> getAllValues() {
		return (List<Object>) queensLocatoins;
	}

	@Override
	public void setValue(int index, Object obj) throws IllegalArgumentException {
		queensLocatoins.set(index, obj);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((queensLocatoins == null) ? 0 : queensLocatoins.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		QueensChromosome other = (QueensChromosome) obj;
		if (queensLocatoins == null) {
			if (other.queensLocatoins != null)
				return false;
		} else if (!queensLocatoins.equals(other.queensLocatoins))
			return false;
		return true;
	}

	@Override
	public String toString() {
		String result = "";
		for(int i=0 ; i < 8 ; ++i)
		{
			for(int j=0 ; j<8 ; j++)
			{
				result+="  - ";
			}
			result+="\n";
			
			for(int j=0 ; j<8 ; j++)
			{
				result+= j == (Integer) queensLocatoins.get(i) ? "| X " : "|   "; 
			}
			result+="|\n";
		}
		for(int j=0 ; j<8 ; j++)
		{
			result+="  - ";
		}
		return result;
	}
	
	
}